package si.unimb.server;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.text.Document;

@SuppressWarnings("serial")
public class ServerHost extends JFrame {
	
	private static final int PRIVZETI_PORT = 1337;
	private int port;
	private JButton button;
	private JTextArea outputArea;
	private ServerListener sl;
	private String log;
	
	public static void main(String[] args) {
		new ServerHost(34000);
	}
	
	public ServerHost(){
		this(PRIVZETI_PORT);
	}
	
	public ServerHost(int port){
		this.port = port;
		log = "";
		init();
	}
	
	public void init(){
		setTitle("Server");
		Container c = getContentPane();
		c.setLayout(new BorderLayout());
		
		outputArea = new JTextArea();
		outputArea.setEditable(false);
		
		button = new JButton("Niti");
		button.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				System.out.println(Thread.activeCount());
			}
		});
		
		c.add(new JScrollPane(outputArea), BorderLayout.CENTER);
		c.add(button, BorderLayout.SOUTH);
		
		setSize(300, 300);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
		
		setSl(new ServerListener(port, this));
		addText("Server online!");
	}
	
	public void addText(String msg){
		log += msg + "\n";
		outputArea.setText(log);
		Document d = outputArea.getDocument();
		outputArea.select(d.getLength(), d.getLength());
	}

	public void setSl(ServerListener sl) {
		this.sl = sl;
	}

	public ServerListener getSl() {
		return sl;
	}
}
